* Multiproduct Firms, Horizontal Mergers, and International Trade
* Jackie M.L. Chan, Michael Irlacher, and Michael Koch

* This do file sets up the dataset used for the regression analysis of domestic market outcomes with the synthetic merger control group.

foreach v in 3c 4c 9c{

use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_dom_mergers_synthetic_02102023_v`v'.dta", clear 
keep if merger==0
keep cvrnrT
duplicates drop
tempfile tempT
save `tempT.dta', replace

********************************************************************************
*** SUM OF ACQUIRER AND TARGET
*Prepare data for target in "wide" format to be merged with A-data
use "D:\Data\Workdata\703989\fjsd3989\STATA FILES\vars_subset.dta", clear 
drop if cvrnr==""
gen vare=substr(vare_nr,1,8)
drop if vare=="00000000"

rename cvrnr cvrnrA
rename aar aarA
rename vare vareA

destring vareA, gen(cn8A)
rename vaerdi vrdA
collapse (sum) vrdA, by(cvrnrA aarA cn8A)

rename vrdA vrdA_vars
merge 1:1 cvrnrA aarA cn8A using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_exports_all_product.dta", keep(match master) keepusing(vrdA) nogen
replace vrdA = 0 if vrdA==.
replace vrdA_vars = vrdA_vars*1000
gen vrdA_dom = vrdA_vars - vrdA

rename cvrnr cvrnrT
rename aarA aarT
rename cn8A cn8T

merge m:1 cvrnrT using `tempT.dta', keep(match) nogen

drop if vrdA_dom<=0
rename vrdA_dom vrdT_dom

collapse (sum) vrdT_dom, by(cvrnrT aarT cn8T)

sort cvrnrT aarT cn8T
by cvrnrT aarT cn8T: gen _prod_distinct = (cn8T~=cn8T[_n-1])
egen nprod = sum(_prod_distinct), by(cvrnrT aarT)
drop _prod_distinct

recast double cn8T 

bysort cvrnrT aarT: gen id = _n
sum nprod
local max=r(max)
foreach i of numlist 1(1)`max'{
gen double cn8T`i'=cn8T if id-1+`i'==nprod
}
foreach i of numlist 1(1)`max'{
gen double vrdT`i'=vrdT_dom if cn8T`i'==cn8T
}
drop cn8T
rename vrdT_dom dom_vrdT
collapse (firstnm) cn8T* (mean) vrdT* (sum) dom_vrdT, by(cvrnrT aarT)
rename dom_vrdT vrdT_dom
append using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_T_vars_dom_prepare_jc_dro.dta"
tempfile temp_T_vars
save `temp_T_vars.dta', replace

********************************************************************************

* Need to expand trade data to create panel for each merger event
use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_dom_mergers_synthetic_02102023_v`v'.dta", clear 
keep cvrnrA aar
duplicates drop
sort cvrnrA aar
* Number of years
by cvrnrA: gen dup2= _n
tempfile temp_did_step2_dup2
save `temp_did_step2_dup2.dta', replace
collapse (max) dup2, by(cvrnrA)
rename dup maxdup2
tempfile temp_did_step2_maxdup2
save `temp_did_step2_maxdup2.dta', replace

*** Add trade data for acquirer ***
* Use A-Date and merge with T-Data
use "D:\Data\Workdata\703989\fjsd3989\STATA FILES\vars_subset.dta", clear 
drop if cvrnr==""
gen vare=substr(vare_nr,1,8)
drop if vare=="00000000"

rename cvrnr cvrnrA
rename aar aarA
rename vare vareA

destring vareA, gen(cn8A)
rename vaerdi vrdA
collapse (sum) vrdA, by(cvrnrA aarA cn8A)

rename vrdA vrdA_vars
merge 1:1 cvrnrA aarA cn8A using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_exports_all_product.dta", keep(match master) keepusing(vrdA) nogen
replace vrdA = 0 if vrdA==.
replace vrdA_vars = vrdA_vars*1000
gen vrdA_dom = vrdA_vars - vrdA

preserve

drop if vrdA_dom<=0

keep cvrnrA aarA cn8A vrdA_dom	
		
merge m:1 cvrnrA using `temp_did_step2_maxdup2.dta', keep(match) nogen
expand maxdup2
bysort cvrnrA aarA cn8A: gen dup2 = _n
merge m:1 cvrnrA dup2 using `temp_did_step2_dup2.dta', keep(match) nogen
drop *dup*
	
gen d_yr = aarA-aar

drop if aar < 1996 | aar > 2015

merge m:1 cvrnrA aar using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_dom_mergers_synthetic_02102023_v`v'.dta", keep(match) nogen

gen aarT = aarA
*Only keep if within the panel sample (i.e., will exclude where acquirer not in VARS and target in VARS)
merge m:1 cvrnrT aarT using `temp_T_vars.dta', keep(match master) nogen

recast double cn8A

egen nprodT=rownonmiss(cn8T*) , strok
sum nprodT
local max=r(max)
gen ind_sp=0
foreach i of numlist 1(1)`max'{
replace ind_sp=1 if cn8A==cn8T`i' & cn8A!=.
}

sort cvrnrA aarA aar cn8A
by cvrnrA aarA aar cn8A: gen _prod_distinct = (cn8A~=cn8A[_n-1])
egen nprod = sum(_prod_distinct), by(cvrnrA aarA aar)
drop _prod_distinct

egen nsp = sum(ind_sp), by(cvrnrA aarA aar)

gen share_nsp=nsp/nprod

egen rankA = rank(vrdA_dom), by(cvrnrA aar aarA) field
rename vrdT_dom dom_vrdT
egen double vrdT_max = rowmax(vrdT*)
foreach i of numlist 1(1)`max'{
gen rankT`i'=.
replace rankT`i'=1 if vrdT`i'==vrdT_max & cn8T`i'!=.
}
foreach i of numlist 1(1)`max'{	
gen samecore`i'=.
replace samecore`i'=1 if rankT`i'==1 & rankA==1 & cn8A==cn8T`i'
}
egen maxsamecore = rowmax(samecore*)

rename dom_vrdT vrdT_dom
collapse nprod nprodT vrdT_dom nsp share_nsp merger (sum) vrdA_dom (mean) maxsamecore, by(cvrnrA aar aarA d_yr) 	
replace d_yr = aarA-aar if d_yr==.
foreach x in vrdA_dom vrdT_dom{
	replace `x' = 0 if `x'==.
	}	
gen long vrdAT_dom = vrdA_dom+vrdT_dom if aarA<=aar
replace vrdAT_dom = vrdA_dom if aarA>aar & merger==1
replace vrdAT_dom = vrdA_dom+vrdT_dom if aarA>aar & merger==0
	
tempfile temp_vars_dom_cvrnr_AT
save `temp_vars_dom_cvrnr_AT.dta', replace
	
********************************************************************************
	
restore
preserve

drop if vrdA_dom<=0

	keep cvrnrA aarA cn8A vrdA_dom	
			
	merge m:1 cvrnrA using `temp_did_step2_maxdup2.dta', keep(match) nogen
	expand maxdup2
	bysort cvrnrA aarA cn8A: gen dup2 = _n
	merge m:1 cvrnrA dup2 using `temp_did_step2_dup2.dta', keep(match) nogen
	drop *dup*
	
	gen d_yr = aarA-aar

	drop if aar < 1996 | aar > 2015

	merge m:1 cvrnrA aar using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_dom_mergers_synthetic_02102023_v`v'.dta", keep(match) nogen

	gen aarT = aarA
	*Only keep if within the panel sample (i.e., will exclude where acquirer not in VARS and target in VARS)
	merge m:1 cvrnrT aarT using `temp_T_vars.dta', keep(match master) nogen
	
	recast double cn8A
	
	keep cvrnrA aar aarA d_yr cn8T* vrdT* merger
	drop vrdT_dom vrdT
	duplicates drop
	reshape long cn8T vrdT, i(cvrnrA aar aarA d_yr) j()
	drop if cn8T==.
	drop _j
	rename vrdT vrdA_dom
	rename cn8T cn8A
 	tempfile temp_vars_dom_cvrnr_T
	save `temp_vars_dom_cvrnr_T.dta', replace
	
	restore
	*Repeat but keep acquirer's products
	drop if vrdA_dom<=0

	keep cvrnrA aarA cn8A vrdA_dom	
			
	merge m:1 cvrnrA using `temp_did_step2_maxdup2.dta', keep(match) nogen
	expand maxdup2
	bysort cvrnrA aarA cn8A: gen dup2 = _n
	merge m:1 cvrnrA dup2 using `temp_did_step2_dup2.dta', keep(match) nogen
	drop *dup*
	
	gen d_yr = aarA-aar

	drop if aar < 1996 | aar > 2015

	merge m:1 cvrnrA aar using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_dom_mergers_synthetic_02102023_v`v'.dta", keep(match) nogen

	gen aarT = aarA
	*Only keep if within the panel sample (i.e., will exclude where acquirer not in VARS and target in VARS)
	merge m:1 cvrnrT aarT using `temp_T_vars.dta', keep(match master) nogen

	recast double cn8A
	
	drop cn8T* vrdT* cvrnrT aarT
	gen acq = 1
	append using `temp_vars_dom_cvrnr_T.dta'
	drop if acq==. & d_yr>0 & merger==1

	collapse (sum) vrdA_dom, by(cvrnrA aarA cn8A aar d_yr)
	bysort cvrnrA aarA aar: gen nprod = _N
		
	**THEIL INDEX**
	egen double help=sum(vrdA_dom), by(cvrnrA aarA aar)
	gen double help2=help/nprod
	gen double help3=vrdA_dom/help2*ln(vrdA_dom/help2)
	egen double help4=sum(help3), by(cvrnrA aarA aar)
	gen double theil_fy=(1/nprod)*(help4)

	drop help*

	gen double max_vrdA_dom = vrdA_dom
	gen double med_vrdA_dom = vrdA_dom
	gen double min_vrdA_dom = vrdA_dom

	gen sdlvrdA_dom = ln(vrdA_dom)

	collapse nprod theil_fy (sum) vrdA_dom (max) max_vrdA_dom (median) med_vrdA_dom (min) min_vrdA_dom (sd) sdlvrdA_dom, by(cvrnrA aarA aar) 
	foreach x in nprod theil_fy vrdA_dom max_vrdA_dom med_vrdA_dom min_vrdA_dom sdlvrdA_dom {
		rename `x' `x'_AT_dro
		}
	
	tempfile temp_vars_dom_cvrnr_AT_val
	save `temp_vars_dom_cvrnr_AT_val.dta', replace
	
	
********************************************************************************

use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_dom_mergers_synthetic_02102023_v`v'.dta", clear 

* Create panel for each merger event
keep cvrnrA aar merger
duplicates drop
expand 7
bysort cvrnrA aar: gen d_yr = _n - 4
gen aarA = aar + d_yr

* Merge with acquirer firm characteristics
rename cvrnrA cvrnr
rename aar aar_ori
rename aarA aar
merge 1:1 cvrnr aar using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_firm_subset1865.dta", keepusing(aar employee_cvrnr CVR_BRANCHE_DB93 CVR_BRANCHE_DB07)

* Only keep relevant firms in this sample, drop all other firms
egen max_merge = max(_merge), by(cvrnr)
keep if max_merge==3
drop max_merge

rename aar aarA
rename aar_ori aar
keep cvrnr aar d_yr aarA employee_cvrnr CVR_BRANCHE_DB07 merger
egen acquirer = max(merger), by(cvrnr)
drop merger

* Match to VARS data
rename cvrnr cvrnrA
gsort cvrnrA -aarA
by cvrnrA: carryforward aar, gen(aar2)
sort cvrnrA aarA
by cvrnrA: carryforward aar2, gen(aar3)
*replace aar3 = aar3+1

rename aar aar_ori
rename aar3 aar
merge 1:1 cvrnrA aarA aar using `temp_vars_dom_cvrnr_AT.dta', keep(match master) nogen

foreach x in nprod{
	rename `x' `x'A
    replace `x'A = 0 if `x'A==.
	replace `x'T = 0 if `x'T==.

	gen `x'AT=0
	replace `x'AT = `x'A + `x'T if aarA<=aar
	replace `x'AT = `x'A if aarA>aar & acquirer==1
	replace `x'AT = `x'A + `x'T if aarA>aar & acquirer==0
	}
	
	gen nprodAT_nodup = nprodA + nprodT - nsp if aarA<=aar
	replace nprodAT_nodup = nprodA if aarA>aar & acquirer==1
	replace nprodAT_nodup = nprodA + nprodT - nsp if aarA>aar & acquirer==0
	
	foreach x in vrdA_dom vrdT_dom vrdAT_dom{
	replace `x' = 0 if `x'==.
	}	
	rename vrdA_dom vrdA_dom_AT
	
merge 1:1 cvrnrA aarA aar using `temp_vars_dom_cvrnr_AT_val.dta', keep(match master) nogen
	
drop aar aar2
rename aar_ori aar

foreach x in theil_fy max_vrdA_dom med_vrdA_dom min_vrdA_dom sdlvrdA_dom{
	gen `x'_AT = `x'_AT_dro
}

gen lnnprodAT=ln(nprodAT)	
gen lnnprodAT_nodup = ln(nprodAT_nodup)
gen lnvrdAT_dom = ln(vrdAT_dom)
gen lnrev_maxAT=ln(max_vrdA_dom_AT)
gen lnrev_medAT=ln(med_vrdA_dom_AT)
gen lnrev_minAT=ln(min_vrdA_dom_AT)

* Indicator for each Indexvalue, except 0 (reference year)
foreach i of numlist 1(1)3{
gen m`i'=0
replace m`i'=1 if d_yr==-`i'
}

foreach i of numlist 1(1)3 {
gen p`i'=0
replace p`i'=1 if d_yr==`i'
}

gen test=0

gen ind=floor(CVR_BRANCHE_DB07/10000)

drop if aarA<1996

egen group_cvrnr = group(cvrnr)
compress
save "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_did_imputation_vars_dom_prepare_jc_synthetic_02102023_AT_v`v'.dta", replace
}